import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

import '../controllers/video_related_controller.dart';
import 'video_share_dialog.dart';

class ShareBottomSheet extends StatelessWidget {
  const ShareBottomSheet({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 10, top: 10, right: 10, bottom: 30),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(20),
          topRight: Radius.circular(20),
        ),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            children: [
              SizedBox(
                width: 10,
              ),
              Text("分享至"),
              Spacer(),
              GestureDetector(
                child: Padding(
                  padding: EdgeInsets.only(right: 2, top: 5),
                  child: Icon(
                    Icons.close,
                    size: 25,
                    color: Colors.grey[400],
                  ),
                ),
                onTap: () {
                  Get.back();
                },
              )
            ],
          ),
          SizedBox(
            height: 10,
          ),
          SizedBox(
            height: 90,
            child: ListView(
              scrollDirection: Axis.horizontal,
              physics: BouncingScrollPhysics(),
              children: [
                _buildShareOption("微信好友", "assets/images/ic_action_share_wechat_grey.png", _shareToWeChat),
                _buildShareOption("微信朋友圈", "assets/images/ic_action_share_moment_grey.png", _shareToMoments),
                _buildShareOption("QQ好友", "assets/images/ic_action_share_qq_grey.png", _shareToQQ),
                _buildShareOption("QQ空间", "assets/images/ic_action_share_qqzone_grey.png", _shareToQQZone),
                _buildShareOption("微博", "assets/images/ic_action_share_weibo_grey.png", _shareToWeibo),
              ],
            ),
          ),
          Divider(
            color: Colors.grey[100],
          ),
          // Second Row: Generate Image and Link Buttons
          Row(
            children: [
              _buildShareOption("生成海报", "assets/images/ic_action_share_download_picture_grey.png", _generateImage),
              _buildShareOption("生成链接", "assets/images/icon_copy_link_grey.png", _generateLink),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildShareOption(String label, String icon, VoidCallback onTap) {
    return SizedBox(
      width: 75,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          IconButton(
            onPressed: onTap,
            icon: Image.asset(
              icon,
              fit: BoxFit.cover,
              width: 40,
            ),
          ),
          Text(
            label,
            style: TextStyle(fontSize: 13),
          ),
        ],
      ),
    );
  }

  void _shareToWeChat() {
    print("Shared to WeChat");
    // Implement WeChat sharing logic
  }

  void _shareToMoments() {
    print("Shared to Moments");
    // Implement Moments sharing logic
  }

  void _shareToQQ() {
    print("Shared to QQ");
    // Implement QQ sharing logic
  }

  void _shareToQQZone() {
    print("Shared to QQ Zone");
    // Implement QQ Zone sharing logic
  }

  void _shareToWeibo() {
    print("Shared to Weibo");
    // Implement Weibo sharing logic
  }

  void _generateImage() {
    Get.dialog(
      VideoShareDialog(),
      useSafeArea: false,
    );
  }

  void _generateLink() async {
    final vc = Get.find<VideoRelatedController>();
    var link = vc.videoInfo.value?.link ?? '';
    await Clipboard.setData(ClipboardData(text: link));
    Fluttertoast.showToast(msg: "链接 已复制", gravity: ToastGravity.CENTER);
  }
}
